package stru1.day6;

/**
 * 383. 赎金信
 */
public class Solution2 {
    public static void main(String[] args) {
        System.out.println(canConstruct("a", "b"));
        System.out.println(canConstruct("aa", "ab"));
        System.out.println(canConstruct("aa", "aab"));
    }

    /**
     * 题目限定字符范围，数组计数
     */
    public static boolean canConstruct(String ransomNote, String magazine) {
        char[] m_array = magazine.toCharArray();
        int[] chars = new int[26];
        //对magazine的字母计数
        for (char c : m_array) {
            chars[c - 'a'] += 1;
        }
        //遍历ransomNote检查各字母数量是否足够
        for (char c : ransomNote.toCharArray()) {
            if ((chars[c - 'a'] -= 1) == -1) return false;
        }

        return true;
    }
}
